<template>
  <div class="review-manager-container" >
    <h2 class="page-title">评价管理</h2>
    
    <!-- 搜索和筛选 -->
    <div style="margin-bottom: 16px; display: flex; gap: 12px; align-items: center;">
      <a-input-search
        v-model:value="searchKeyword"
        placeholder="搜索酒店名称或用户名称"
        style="width: 300px;"
        @search="handleSearch"
        enter-button
      />
      <a-select
        v-model:value="ratingFilter"
        placeholder="评分筛选"
        style="width: 120px;"
        @change="handleFilter"
        allow-clear
      >
        <a-select-option value="">全部</a-select-option>
        <a-select-option value="5">5星</a-select-option>
        <a-select-option value="4">4星</a-select-option>
        <a-select-option value="3">3星</a-select-option>
        <a-select-option value="2">2星</a-select-option>
        <a-select-option value="1">1星</a-select-option>
      </a-select>
      <a-button type="primary" @click="loadReviews">刷新</a-button>
    </div>

    <!-- 评价列表 -->
    <a-table
      :columns="columns"
      :data-source="filteredReviews"
      :loading="loading"
      :pagination="pagination"
      row-key="id"
      @change="handleTableChange"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'rating'">
          <a-rate :value="Number(record.rating)" disabled />
          <span style="margin-left: 8px; color: #faad14;">{{ record.rating }}分</span>
        </template>
        <template v-else-if="column.key === 'content'">
          <div style="max-width: 300px; word-break: break-word;">
            {{ record.content || '无评价内容' }}
          </div>
        </template>
        <template v-else-if="column.key === 'createdAt'">
          {{ formatDate(record.createdAt) }}
        </template>
        <template v-else-if="column.key === 'action'">
          <a-space>
            <a-button type="link" size="small" danger @click="deleteReview(record.id)">
              删除
            </a-button>
          </a-space>
        </template>
      </template>
    </a-table>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { message } from 'ant-design-vue'
import request from '@/utils/request'

// 响应式数据
const reviews = ref([])
const loading = ref(false)
const searchKeyword = ref('')
const ratingFilter = ref('')

// 分页配置
const pagination = ref({
  current: 1,
  pageSize: 10,
  total: 0,
  showSizeChanger: true,
  showQuickJumper: true,
  showTotal: (total) => `共 ${total} 条评价`
})

// 表格列配置
const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    key: 'id',
    width: 80
  },
  {
    title: '酒店名称',
    dataIndex: 'hotelName',
    key: 'hotelName',
    width: 200
  },
  {
    title: '用户名称',
    dataIndex: 'userName',
    key: 'userName',
    width: 120
  },
  {
    title: '评分',
    dataIndex: 'rating',
    key: 'rating',
    width: 150
  },
  {
    title: '评价内容',
    dataIndex: 'content',
    key: 'content',
    ellipsis: true
  },
  {
    title: '创建时间',
    dataIndex: 'createdAt',
    key: 'createdAt',
    width: 180
  },
  {
    title: '操作',
    key: 'action',
    width: 100,
    fixed: 'right'
  }
]

// 过滤后的评价列表
const filteredReviews = computed(() => {
  let filtered = reviews.value

  // 关键词搜索
  if (searchKeyword.value) {
    const keyword = searchKeyword.value.toLowerCase()
    filtered = filtered.filter(review => 
      (review.hotelName && review.hotelName.toLowerCase().includes(keyword)) ||
      (review.userName && review.userName.toLowerCase().includes(keyword)) ||
      (review.content && review.content.toLowerCase().includes(keyword))
    )
  }

  // 评分筛选
  if (ratingFilter.value) {
    filtered = filtered.filter(review => review.rating === ratingFilter.value)
  }

  return filtered
})

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return '-'
  try {
    return new Date(dateString).toLocaleString('zh-CN')
  } catch {
    return dateString
  }
}

// 加载评价列表
const loadReviews = async () => {
  loading.value = true
  try {
    const res = await request.get('/po/reviews/all', {
      params: {
        page: pagination.value.current,
        size: pagination.value.pageSize
      }
    })
    
    const data = res.data?.data || {}
    reviews.value = data.list || []
    pagination.value.total = data.total || 0
  } catch (error) {
    console.error('加载评价列表失败:', error)
    message.error('加载评价列表失败')
  } finally {
    loading.value = false
  }
}

// 搜索处理
const handleSearch = () => {
  // 搜索逻辑在computed中处理，这里只需要触发重新计算
}

// 筛选处理
const handleFilter = () => {
  // 筛选逻辑在computed中处理，这里只需要触发重新计算
}

// 表格变化处理
const handleTableChange = (pag) => {
  pagination.value.current = pag.current
  pagination.value.pageSize = pag.pageSize
  loadReviews()
}

// 删除评价
const deleteReview = async (reviewId) => {
  try {
    await request.delete(`/po/reviews/${reviewId}`)
    message.success('删除成功')
    loadReviews()
  } catch (error) {
    console.error('删除评价失败:', error)
    message.error('删除评价失败')
  }
}

// 组件挂载时加载数据
onMounted(() => {
  loadReviews()
})
</script>

<style scoped>
.ant-table-tbody > tr > td {
  vertical-align: top;
}
</style>
